package com.wjj.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.infrastructure.utils.ScreenUtils;
import com.wjj.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wjj on 2016/12/19.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
    private List<String> mList = new ArrayList<>();
    private Context mContext;

    public RecyclerViewAdapter(List<String> list, Context context) {
        this.mList = list;
        this.mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_rcdemo_rc, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

//        holder.imageView.setImageURI(Uri.parse("http://e.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=f525e05ccf11728b30788426fdcceff8/203fb80e7bec54e712f475d3bb389b504ec26ae6.jpg"));
        holder.textView.setText(mList.get(position));
        if (position % 2 == 0) {
            holder.rootView.setPadding(0, 0, 5, 10);
        } else {
            holder.rootView.setPadding(5, 0, 0, 10);
        }


        int screenWidth = ScreenUtils.getScreenWidth(mContext);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.imageView.getLayoutParams();
        params.width = screenWidth / 2-5;
        double h=(screenWidth / 2-5)/0.98;
        params.height = (int)h;
        holder.imageView.setLayoutParams(params);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;
        private FrameLayout rootView;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.item_iv);
            textView = (TextView) itemView.findViewById(R.id.item_tv);
            rootView = (FrameLayout) itemView.findViewById(R.id.rootview);
        }
    }
}
